<?php
class Itinerary_model extends Model {

    function Itinerary_model()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function create_or_update($itinerary, $tourtype_id = null, $tour_id = null, $bus_number = null)
    {
        // First delete the existing itinerary
		if ($tourtype_id != null)
		{
			$this->db->where('tourtype_id =', $tourtype_id);		
			$this->db->delete('itineraries');
		} elseif ($tour_id != null && $bus_number != null)
		{
			$this->db->where(array('tour_id' => $tour_id, 'bus_number' => $bus_number));		
			$this->db->delete('itineraries');
		}
		
		// Add a new itinerary
		
		for ($i = 1; $i <= count($itinerary); $i++)
		{
			$data = array(
               'night_number' => $i,
               'hostel_id' => $itinerary[$i],
            );
			
			if ($tourtype_id != null)
			{
				$data['tourtype_id'] = $tourtype_id;
			} elseif ($tour_id != null && $bus_number != null)
			{
				$data['tour_id'] = $tour_id;
				$data['bus_number'] = $bus_number;
			}
			
			$this->db->insert('itineraries', $data); 
		}
		
		
    }
	
	function delete_itinerary($tourtype_id = null, $tour_id = null, $bus_number = null)
    {
        // First delete the existing itinerary
		if ($tourtype_id != null)
		{
			$this->db->where('tourtype_id =', $tourtype_id);		
			$this->db->delete('itineraries');
		} elseif ($tour_id != null && $bus_number != null)
		{
			$this->db->where(array('tour_id' => $tour_id, 'bus_number' => $bus_number));		
			$this->db->delete('itineraries');
		}
	
	}
	
	function get_itinerary($tourtype_id = null, $tour_id = null, $bus_number = null)
	{
		$this->db->select('night_number, hostel_id, hostel_name');
		
		if ($tourtype_id != null)
		{
			$this->db->where('tourtype_id =', $tourtype_id);		
		} elseif ($tour_id != null && $bus_number != null)
		{
			$this->db->where('tour_id =', $tour_id);
			$this->db->where('bus_number =', $bus_number);
		}
		
		$this->db->where('hostel_id = id');
		
		$this->db->order_by('night_number', 'asc');
		$query = $this->db->get('itineraries, hostel_locations');
		
		// Add in a join to get the hostel name too
		
		return $query->result_array();
	}
	
	

}
?>